/**
  ******************************************************************************
  * @file           : studio_color.h
  * @author         : wangyingjie
  * @brief          : None
  * @attention      : None
  * @date           : 2025/6/4
  ******************************************************************************
  */

#ifndef STUDIO_COLOR_H
#define STUDIO_COLOR_H

#include "studio_macros.h"

#define COLOR_MAX_D 255.0

class studio_color
{
public:
    studio_color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
        : red(r),
          green(g),
          blue(b),
          alpha(a)
    {
    }

    studio_color(uint8_t r, uint8_t g, uint8_t b)
        : red(r),
          green(g),
          blue(b)
    {
    }

    studio_color(uint8_t g, uint8_t a)
        : alpha(a),
          gray(g)
    {
    }

    studio_color(uint8_t g)
        : gray(g)
    {
    }

    studio_color()
    {
    }

public:
    enum type : uint8_t
    {
        eptRGB = 1,  // PNG_COLOR_TYPE_RGB,
        eptRGBA = 2, // PNG_COLOR_TYPE_RGB_ALPHA,
        eptGRAY = 3, // PNG_COLOR_TYPE_GRAY,
        eptGRAYA = 4 // PNG_COLOR_TYPE_GRAY_ALPHA
    };

public:
    static int channels(const type& t);

    /// <summary>
    /// 从16进制字符串加载颜色  如 ABE0457B
    /// </summary>
    /// <param name="color"></param>
    bool hex2argb(const char* hex);
    bool hex2rgb(const char* hex);
    bool hex2rgba(const char* hex);

    bool hex2argb(const std::string& hex)
    {
        return hex2argb(hex.c_str());
    }

    bool hex2rgb(const std::string& hex)
    {
        return hex2rgb(hex.c_str());
    }

    bool hex2rgba(const std::string& hex)
    {
        return hex2rgba(hex.c_str());
    }

    /// <summary>
    /// 颜色值转16进制字符串
    /// </summary>
    /// <returns></returns>
    std::string argb2hex() const;

    std::string rgb2hex() const;

    std::string rgba2hex() const;

    std::string abgr2hex() const;

    std::string bgra2hex() const;

    std::string bgr2hex() const;

    /// <summary>
    /// 无损表示-12799.9999 到 12799.9999 之间的值
    /// 这个数值范围在水文,气象领域内应该是足够用的
    /// 使用改实现方式可以在shader方便恢复
    /// </summary>
    /// <param name="v"></param>
    void vencode(const double& v);

    /// <summary>
    /// 恢复为double
    /// </summary>
    /// <returns></returns>
    double vdecode() const;

public:
    uint8_t gray{0};
    uint8_t red{0};
    uint8_t green{0};
    uint8_t blue{0};
    uint8_t alpha{0};
};


#endif //STUDIO_COLOR_H